package org.itboys.core.service.mysql.web;


import org.apache.commons.lang3.StringUtils;

import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.core.common.ExceptionEnum;
import org.itboys.core.entity.mysql.SundayProduct;
import org.itboys.mysql.service.BaseMySqlService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.*;


/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_商品_service_web
 * 当前为单规格。如果需要多规格，请自行扩展
 */
@Service
public class SundayProductService extends BaseMySqlService<SundayProduct> {
	@Autowired
	private SundayUploadService uploadService;
	/**
	 * 新增
	 * @param product
	 * @param rollImageFiles
	 * @throws IOException
	 */
	@Transactional
	public void save(
			SundayProduct product,
			MultipartFile backImageFile,
			MultipartFile defaultImageFile,
			MultipartFile[] rollImageFiles
	) throws  Exception {

		Map<String, String> backMap = uploadService.uploadSingle(backImageFile);
		if(StringUtils.isNotEmpty(backMap.get(uploadService.UPLOAD_HALF_PATH))){
			product.setBackImage(backMap.get(uploadService.UPLOAD_HALF_PATH));
		}
		Map<String, String> defaultMap = uploadService.uploadSingle(defaultImageFile);
		if(StringUtils.isNotEmpty(defaultMap.get(uploadService.UPLOAD_HALF_PATH))){
			product.setDefaultImage(defaultMap.get(uploadService.UPLOAD_HALF_PATH));
		}
		//编辑
		if(LongUtil.isNotZreo(product.getId())){
			super.update(product);
		}else{
			super.insert(product);
		}
		//保存轮播图
		uploadService.uploadMultiWithRecord(rollImageFiles,product.getId(),SundayProduct.class.getSimpleName(),"商品ID"+product.getId()+",的轮播图");

	}

	/**
	 * 分页查询商品
	 * @param request
	 * @return
	 */
	public PageResult<SundayProduct> selectProduct(HttpServletRequest request) throws Exception {
		throw new ErrorException(ExceptionEnum.COMMENEXCPTION.getValue());
	/*	Map<String, Object> param = ParamUtil.packageMysqlPageParam(request, "name","status","categoryId");
		return new PageResult<SundayProduct>(super.select(param), super.count(param));*/

	}


	/**
	 * 查询商品详情。后台
	 * @param productId
	 * @return
	 */
	public SundayProduct findOne(Long productId){
		SundayProduct product=null;
		if(LongUtil.isNotZreo(productId)){
			product = super.find(productId);
		}else {
			product=new SundayProduct();
		}
		return product;
	}


}

